package br.com.rookgraph.run;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;

import br.com.rookgraph.Graph;
import br.com.rookgraph.utils.BranchReduceDepurator;



/**
 * Execulta as heurísticas a partir de gra
 * @author Luan Pontes
 *
 */
public class DepureBranchReduceForFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		BufferedReader input = prepareInput(args[0]);

		try {

			int size = Integer.parseInt(input.readLine());
			Graph grafo = new Graph(size);

			String aresta;
			String[] vertices;
			do{
				aresta = input.readLine();
				if(aresta != null){
					vertices = aresta.split(" ");
					if (vertices != null) {
						grafo.addEdge(Integer.valueOf(vertices[0]), Integer.valueOf(vertices[1]), 1);
					}
				}
			}while(aresta != null);


			input = prepareInput(args[1]);

			int sizeTree = Integer.parseInt(input.readLine());
			Graph tree = new Graph(size);

			do{
				aresta = input.readLine();
				if(aresta != null){
					vertices = aresta.split(" ");
					if (vertices != null) {
						tree.addEdge(Integer.valueOf(vertices[0]), Integer.valueOf(vertices[1]), 1);
					}
				}
			}while(aresta != null);


			System.out.println("\t Modo Depuracao");
			System.out.println("***************** Heurísticas *********");
			System.out.println("* 1 - Normal  ==> Estanque            *");
			System.out.println("* 2 - Alterado ==> Estanque           *");
			System.out.println("* 3 - Plus ==> Estanque               *");
			System.out.println("* 4 - Não Estanque + Plus2            *");
			System.out.println("* 5 - Não Estanque + Ultima Heuristica*");
			System.out.println("*************************************** ");
			System.out.println(" Digite a Herística que deseja utilizar:  ");
			int op = new Scanner(System.in).nextInt();


			switch (op) {

				case 1:
					BranchReduceDepurator.normal(grafo, tree);
					break;

				case 2:
					BranchReduceDepurator.alterado(grafo, tree);
					break;

				case 3:
					BranchReduceDepurator.plus(grafo, tree);
					break;
					
				case 4:
					BranchReduceDepurator.plus2(grafo, tree);
					break;
					
				case 5:
					BranchReduceDepurator.notestanque(grafo, tree);
					break;

				default:
					System.out.println("DIGTE OPÇÂO VÀLIDA!");
					break;
			}

		} catch (IOException e) {
			e.printStackTrace();
		}


	}

	/**
	 * Prepara a leitura  com base no args do programa.
	 * O primeiro args especifica o arquivo de entrada,
	 * se ele estiver null, adimite a entrada padrão.
     *
	 * @param args
	 * @return
	 */
	private static BufferedReader prepareInput(String args) {
		try {

			Reader inputStreamReader = args == null ? new InputStreamReader(System.in) : new FileReader(new File(args));

			BufferedReader input = new BufferedReader(inputStreamReader);

			return input;

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		return null;

	}

	/**
	 * Busca dentre as árvores que podem ser geradas a
	 * partir do grafo original a que possue o maior
	 * número de branches.
	 * @param graph
	 *
	 * @return
	 */
	private Graph findMaxBrachTree(Graph graph) {
		Graph currentTree;

		Graph mininumSpanningTree = graph.getTree(new Graph(graph.getOrder()), 1);;

		for (int i = 2; i <= graph.getOrder(); i++) {

			currentTree = graph.getTree(new Graph(graph.getOrder()), i);

			int currentLength = currentTree.getBranchs().length;
			int maxLength = mininumSpanningTree.getBranchs().length;

			if(maxLength < currentLength){
				mininumSpanningTree = currentTree;
			}

		}
		return mininumSpanningTree;
	}

}
